package com.mooc.yohoshop.ui.user;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mooc.yohoshop.R;
import com.mooc.yohoshop.annotation.CheckLogin;
import com.mooc.yohoshop.dao.UserDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.UserEntity;
import com.mooc.yohoshop.entity.UserValuesEntity;
import com.mooc.yohoshop.filed.EventAction;
import com.mooc.yohoshop.imgload.ImageLoad;
import com.mooc.yohoshop.ui.base.BaseLazyFragment;
import com.mooc.yohoshop.ui.login.LoginActivity;
import com.mooc.yohoshop.ui.order.OrderActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class UserFragment extends BaseLazyFragment<UserRepo> implements View.OnClickListener {

    private TextView mUserNameTv;
    private ImageView mUserIconIv;
    private UserDaoEntity userDaoEntity = new UserDaoEntity();
    @Override
    protected void lazyInit() {
        List<UserValuesEntity> list = userDaoEntity.selectAll();
        if (list.size()>0){
            mUserNameTv.setText(list.get(0).nick_name);
            ImageLoad.loadCircle(list.get(0).user_head,mUserIconIv);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshUser(EventAction action){
        if (action.action.equals("refreshUser"))
            lazyInit();
    }

    @Override
    protected int layout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        mUserNameTv = f(R.id.mUserNameTv);
        mUserIconIv = f(R.id.mUserIconIv);
        mUserNameTv.setOnClickListener(this::onClick);
        f(R.id.mWaitPayOrderTv).setOnClickListener(this::onClick);
        f(R.id.mWaitConfirmOrderTv).setOnClickListener(this::onClick);
        f(R.id.mCompleteOrderTv).setOnClickListener(this::onClick);
        f(R.id.mAllOrderTv).setOnClickListener(this::onClick);
    }

    @Override
    protected void result(BaseEntity baseEntity) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.mWaitPayOrderTv:
            case R.id.mWaitConfirmOrderTv:
            case R.id.mCompleteOrderTv:
            case R.id.mAllOrderTv:
                startOrder();
                break;
            case R.id.mUserNameTv:
                startActivity(new Intent(getContext(), LoginActivity.class));
                break;
        }
    }
    @CheckLogin
    private void startOrder(){
        startActivity(new Intent(getContext(), OrderActivity.class));
    }
}
